package com.yingxin.yxerp.module.organization.service.helper;

import com.yingxin.yxerp.module.dingtalk.entity.FieldTypeEnum;
import com.yingxin.yxerp.module.dingtalk.myinterface.DingUserFieldCode;
import com.yingxin.yxerp.module.dingtalk.vo.DingFieldValueVo;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

/**
 * 将 DingUserRespVo 注解的 erpFieldName属性名 赋值给 user 的属性
 * yuebao
 * 23-04-18
 */

public class UserCopyDingUserAttributeHelper {

    /**
     * 将 DingUserRespVo 注解的 erpFieldName属性名 赋值给 user 的属性
     * @param user
     * @param dingUserRespVo
     */
    public static void reflectionUserInfo(Object user, Object dingUserRespVo) throws IllegalAccessException {
        // 获取user的所有属性
        Field[] userFields = user.getClass().getDeclaredFields();

        // 获取 dingUserRespVo 的所有属性
        Field[] dingUserFields = dingUserRespVo.getClass().getDeclaredFields();

        // 循环 user 的所有属性
        for( Field userField : userFields ){
            // 设置属性可访问
            userField.setAccessible( true );

            // 循环 dingUserRespVo 的所有属性
            for( Field dingUserField : dingUserFields ){
                // 设置属性可访问
                dingUserField.setAccessible( true );
                try {
                    if (dingUserField.isAnnotationPresent(DingUserFieldCode.class)) {
                        // 获取 注解
                        DingUserFieldCode dingUserFieldCode = dingUserField.getAnnotation( DingUserFieldCode.class );
                        if( StringUtils.isNotEmpty( dingUserFieldCode.erpFieldName().trim() ) ){
                            if( userField.getName().trim().equals( dingUserFieldCode.erpFieldName().trim() ) ){
                                // 文本
                                if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.TextField.getType() ) ){
                                    userField.set(user, dingUserField.get( dingUserRespVo ) );
                                }
                                // 选项类型
                                if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDSelectField.getType() ) ){
                                    if( "staffPositionId".trim().equals( dingUserFieldCode.erpFieldName().trim() ) ){
                                        DingFieldValueVo vo = (DingFieldValueVo) dingUserField.get( dingUserRespVo );
                                        userField.set(user, vo.getLabel() );
                                    }else{
                                        DingFieldValueVo vo = (DingFieldValueVo) dingUserField.get( dingUserRespVo );
                                        userField.set(user, vo.getValue() );
                                    }
                                }
                                // 日期类型
                                if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDDateField.getType() ) ){
                                    try {
                                        userField.set( user, dingUserField.get( dingUserRespVo ) );
                                    }catch (Exception e){ }
                                }
                                // 长日期类型
                                if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDDateWithLongField.getType() ) ){
                                    try {
                                        userField.set( user, dingUserField.get( dingUserRespVo ) );
                                    }catch (Exception e){ }
                                }
                                // 图片类型
                                if( dingUserFieldCode.fieldType().trim().equals( FieldTypeEnum.DDPhotoField.getType() ) ){
                                    userField.set( user, dingUserField.get( dingUserRespVo ) );
                                }
                            }
                        }
                    }
                }catch (Exception e){
                }
            }
        }
    }

}
